package cn.edu.lingnan.servlet;

import cn.edu.lingnan.pojo.Competition;
import cn.edu.lingnan.service.ICompetitionService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/admin/addCompete")
public class addCompeteServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取表单数据
        String comName = request.getParameter("comName");
        String comDesc = request.getParameter("comDesc");
        String comStartTime = request.getParameter("comStartTime");
        String comEndTime = request.getParameter("comEndTime");
        Competition competition = new Competition(comName, comDesc, comStartTime, comEndTime);
        System.out.println("[Debug 075] 成功进入页面!");
        // 调用 Service 层方法添加比赛项目
        ICompetitionService service = new ICompetitionService();
        boolean success = service.addCompetition(competition);

        // 处理结果
        if (success) {
            // 添加成功，跳转到比赛项目列表页面
            response.sendRedirect("/queryAllCompete");
        } else {
            // 添加失败，跳转到错误页面或显示错误信息
            response.sendRedirect("/AddCompeteError.html");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

